package com.chenqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenqi.dao.UserMapper;
import com.chenqi.model.dto.RegisterDTO;
import com.chenqi.model.entity.User;
import com.chenqi.service.UserService;
import com.chenqi.utils.JwtUtils;
import com.chenqi.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @description: 用户业务实现
 * @program: ardaicommunity
 * @author: ChenQi
 * @create: 2021-03-2021/3/8-08-50
 */
@Slf4j
@Service
@Transactional(rollbackFor = {Exception.class})
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int registerUser(RegisterDTO registerDTO) {
        User user = new User();
        user.setUsername(registerDTO.getUsername())
                .setAlias("commonUser")
                .setSign("这是一段有趣的签名")
                //对用户输入的密码进行加密
                .setPassword(MD5Utils.code(registerDTO.getPass()))
                .setAvatar("https://s3.ax1x.com/2020/12/01/DfHNo4.jpg")
                .setEmail(registerDTO.getEmail())
                .setScore(0)
                .setBio("学生")
                .setActive(1)
                .setStatus(1)
                .setCreateTime(new Date())
                .setModifyTime(new Date());
        int i = userMapper.insert(user);
        return i;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public User findUserByUsernameAndPassword(String username,String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public String login(String username,String password) {
        User user = findUserByUsernameAndPassword(username, MD5Utils.code(password));
        if (user != null) {
            String token = JwtUtils.geneJsonWebToken(user);
            return token;
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public User findUserById(Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public User findUserByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int updateUser(User user) {
        int i = userMapper.updateById(user);
        return i;
    }
}
